package com.apci.ipreda.client.web.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractFormController;
import com.apci.ipreda.client.web.form.DeclaracionForm;
import com.apci.ipreda.domain.Entidad;
import com.apci.ipreda.security.domain.Usuario;
import com.apci.ipreda.service.UsuarioService;

public class DeclaracionBusquedaController extends AbstractFormController {
	private UsuarioService usuarioService;

	public void setUsuarioService(UsuarioService usuarioService) {
		this.usuarioService = usuarioService;
	}

	protected ModelAndView processFormSubmission(HttpServletRequest request,
			HttpServletResponse response, Object object,
			BindException paramBindException) throws Exception {
		String logonId = request.getParameter("logonId");
		Usuario usuario = this.usuarioService.getUsuarioByUsername(logonId);

		if ((usuario!=null) && (usuario.getEntidad() != null) && (usuario.getEntidad().getId() != null)) {
			Entidad entidad = usuario.getEntidad();
			DeclaracionForm declaracionForm = (DeclaracionForm) object;
			declaracionForm.getDeclaracion().setEntidad(entidad);
		}

		return showForm(request, paramBindException,
				"busqueda/declaracionBusquedaList");
	}

	protected ModelAndView showForm(HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse,
			BindException paramBindException) throws Exception {
		return showForm(paramHttpServletRequest, paramBindException,
				"busqueda/declaracionBusquedaList");
	}

	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		this.logger.info("----------------referenceData---------------");
		Map<String, Object> referenceData = new HashMap<String, Object>();
		DeclaracionForm declaracionForm = (DeclaracionForm)command;
		
		referenceData.put("declaracionForm", declaracionForm);
		return referenceData;
	}
}